/*
 * @Author: wangjing wangjin122@qq.com
 * @Date: 2024-02-23 16:45:24
 * @LastEditors: wangjing wangjin122@qq.com
 * @LastEditTime: 2024-02-23 17:13:42
 * @FilePath: \泛型\index2.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

/**
 * 泛型约束
 *  在类型后面跟  extends 约束类型  
*/
// extends 示例1
function addFn<T extends number>(a: T, b: T): number {
  return a + b
}
addFn(1,2)

// extends 示例2
interface Len {
  length: number
}
function fn<T extends Len> (a: T) {
  a.length
}
fn('1234')

// keyof 的使用
let obj = {
  name: '坤坤',
  sex: '女'
}
// type key = keyof typeof obj // 直观解释keyof
function ob<T extends object, K extends keyof T>(obj: T, key: K) {
  return obj[key]
}
ob(obj,'name')

// keyof 的高级用法
interface Data1 {
  name: string,
  age: number,
  sex: string
}
type Options<T extends object> = {
  [Key in keyof T]?: T[Key]
}
type B = Options<Data1>